// addressPicker.js
var app = getApp()
Component({
  mixins: [],
  data: {
	alipcd: [0, 0, 0],
    pcd: ['广东省', '广州市', '海珠区'],
    addressList: [], // 所有的地址信息数据
    province: [],
    city: [],
    county: [],
    apptype: app.setting.apptype,
  },
  props: {
    visible: Boolean,
    pcd : {
      type : Array,
      value : []
    },
  },
  properties: {
	 pcd : {
      type : Array,
      value : []
    }, 
  },
  didMount() {
	 this.setData({
		pcd: this.props.pcd,
	})
    this.getProvinceList()
  },
  didUpdate() {},
  didUnmount() {},
  methods: {
    getProvinceList() {
      let that = this
	  //console.log('city');console.log(that.data.city)
	  if(app.setting.apptype=='aliapp'){
		  app.util.get('core/member.get_data/getprovince/city='+that.data.city+'&district='+that.data.district,function(res){
			  //console.log('getprovince');console.log(res)
			   that.setData({
				addressList: res.province,
			  })
		  });
	  }
    },
   onChange(e) {
	  console.log('onChange');
      let currentValue = e.detail.value;
      let alipcd = this.data.alipcd;
      let resultValue = [];
      if (currentValue[0] != alipcd[0]) {
        resultValue = [currentValue[0], 0, 0];
      } else if (currentValue[1] != alipcd[1]) {
        resultValue = [currentValue[0], currentValue[1], 0];
      } else {
        resultValue = currentValue;
      }
      this.setData({
        alipcd: resultValue
      });
    },
	wxChange(e){
		var pcd = e.detail.value
		this.setData({
		  'pcd':pcd,
		})
		let obj = {
			type: 'confirm',
			pcd: pcd
		}
		this.setData({
			pcd: pcd
		});
		console.log('wxChange');
		this.triggerEvent('pcdchange',obj)
    },
	setvisible(){
	  var visible=visible?false:true;
      this.setData({
        visible: visible
      });
    },
    /**
     * 点击取消操作，返回cancel操作，value为空，不返回任何值
     */
    clickCancel() {
      let obj = {
        type: 'cancel',
        pcd: ''
      }
	  this.setData({
        visible: false
      });
      this.props.onChange(obj);
    },
    /**
     * 点击确定操作，根据当前结果值，返回对应的name
     */
    clickConfirm() {
      // 遍历当前结果值，根据当前结果值索引，拿到地址数据
      let addressList = this.data.addressList;
	  let alipcd = this.data.alipcd;
	  let pcd = [];
	  pcd[0] = addressList[alipcd[0]].name; // 省
      pcd[1] = addressList[alipcd[0]].areas[alipcd[1]].name; // 市
      pcd[2] = addressList[alipcd[0]].areas[alipcd[1]].areas[alipcd[2]].name; // 区
      let obj = {
        type: 'confirm',
        pcd: pcd
      }
	  this.setData({
        visible: false,
		pcd: pcd
      });
	  //console.log(this.props);
      this.props.onChange(obj)
    }
  }
});


